오늘한 일
- Rust 공부
- Android UI 구현
- 새로운 카공메이트 GET
Rust 공부
Closure
: 익명함수
let add = | a:i32, b:i32 | -> i32 {
a + b
};
OR
let add = | a, b | a + b ;
→ 함수와 거의 동일
함수와 거의 동일하지만 사용하는 이유
- 타입추론으로 작성이 편함, 가독성이 좋음
- 다른 함수의 인자로 넘기기 편함
Map combinator
매핑: 데이터를 다룰 때, 어떤 데이터를 원하는 데이터로 변환하는 것
.map()
: Option 값 패턴 매칭의 단순화
fn maybe_num(): Option<i32> {}
let word_len = maybe_num().map(|len| len + 1);
: 클로저를 내부 함수로 사용,
Some → 내부 함수 사용
None → 내부 함수 사용 X
⇒ return 값 또한 Option 값
EXAMPLE
#[derive(Debug)]
struct User {
user_id: i32,
name: String,
}
/// Locates a user id based on the name.
fn find_user(name: &str) -> Option<i32> {
match name.as_str() {
"sam" => Some(1),
"matt" => Some(5),
_ => None,
}
}
fn main() {
find_user("matt")
.map(|id| User{ user_id: id, name: "matt".to_owned()})
.map(|user| println!("{:?}", user));
}
Module
:함수를 저장, 함수를 가져와서 사용하는게 용이
mod math{
fn add(a:i32, b:i32) -> i32 {
a+b
}
fn sub(a:i32, b:i32) -> i32 {
a-b
}
}
fn main() {
use math::add;
use math::*;
-> // add() 사용가능 or math::add()로 사용
}
모듈화
: 기능별로 함수나 자료구조를 묶어서 사용 → 유지보수용이
각각의 모듈은 별개의 파일처럼 생각하면 된다.
Testing
Rust에서 Test 방식
: 프로그램에서 나가게 되면 테스트에 실패한다.
→ assert_eq!(RESULT, EXPECTED, MESSAGE)
사용
#[cfg(test)]
: 컴파일러가 test를 위한 코드임을 알 수 있다.
#[cfg(test)]
mod test{
use crate::*;
#[test]
fn check_FN(){
let result = FN();
let expected = VALUE;
assert_eq!(result, expected, "NOT FOUND");
}
#[test]
fn check_FN2(){
let result = FN();
let expected = VALUE2;
assert_eq!(result, expected, "NOT FOUND");
}
test
- assert_eq!
가 하나의 세트
: mod는 별도의 파일이므로 외부의 함수를 가져오기 위해서 use crate::FN
사용
Option combinator
let OPTION: Option<T>;
OPTION.is_some(); -> bool
OPTION.is_none(); -> bool
OPTION.map(|args| body ); -> new T
OPTION.filter(|args| Statement ); -> Option<T>
OPTION.or_else(|| Option<T> ); -> new Option<T>
OPTION.unwrapped_or_else(|| T ); -> T
*filter는 inner data를 빌려쓴다는 점 주의 → Statement 만들 때 &값 이용
Iterator
: 반복 구조 순회에 도움되는 함수
let nums = vec![1,2,3,4,5];
let new_nums: Vec<_> = nums
.iter()
.map(|num| num + 1 )
.collect();
.iter()
로 반복 조회 → ‘어떤 연산’ 실행
.map()
’어떤 연산’에 해당
.collect()
로 새로운 벡터 생성 → collect는 벡터에만
Vec<_>
가 필요한 이유
어떤연산 모음
.iter()
//chaining 가능
.map(||);
.filter(||);
.take(NUMBER_TO_GET);
.find(||); -> Option<T>
.last();
.count();
.min();
.max();
.collect();
iter()
의 의미 = 컴파일러에게 순회를 할 것이라는 언질을 해주는 역할로 생각
range → collection
let ch = 'a'..='f';
let num = 1..100;
for num in 1..100{}
일종의 collection 생성
..
는 마지막 제외, ..=
는 마지막 포함
Android UI 구현중 이슈 정리
ImagePicker 만들기
*코드 외부 리소스 사용 명시
레이아웃 단 : 기본이미지 사용, ImageView로 화면에 띄우기
Activity.kt 단: Image 띄울 때, MotionEvent 확인으로 터치시 이벤트 핸들링 → new image
이미지 클릭 → MotionEvent로 발생 → 핸들링해서 new image를 추출하는 로직 실행 → new image로 ImageView 대체
ScrollView 크기를 ConstraintLayout에 맡기기
: only 레이아웃단
android:layout_height="0dp"
사용시, 해당 컴포넌트의 height은 Cosntraint에 의해 결정된다.
__
The android:layout_height="0dp"
is used with the ConstraintLayout
to create a height constraint that will be resolved based on the constraints applied to the view.
__
기타..
android:scrollbars="none"
→ 스크롤바 없애기
ConstraintLayout 원하는대로 다루기
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent="0.5" />
→ constraint의 새로운 기준으로 사용 가능 by constraintGuide_percent
⇒ Guideline 정리
특성
Invisibility: Guideline
is not visible when the app is run
Orientation: A Guideline
can be either horizontal or vertical, depending on the layout's needs. determined by the app:layout_constraintOrientation
attribute
Position: The position of a Guideline
is defined using one of two attributes:
app:layout_constraintGuide_percent
→ (0.0
to 1.0
)
app:layout_constraintGuide_begin
→ pixel offset (dp
)
Placement: Views can be aligned or constrained relative to a Guideline
using the appropriate ConstraintLayout
attributes. → Dynamic Positioning
→ 재욱님은 ConstrintLayout에서 Guideline으로 절대적 위치를 잡고 시작하는걸 좋아하시는지 Widget간 상대적 위치로 표현하는 걸 좋아하시는지 궁금